package com.dosh;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.SeriesSelection;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class PieChartBuilder extends Activity {
	/**
	 * necessary elements defined in the library
	 */
  public static final String TYPE = "type";
  
  DBAdapter db = new DBAdapter(this);

  private static int[] COLORS = new int[] { Color.GREEN, Color.BLUE, Color.MAGENTA, Color.CYAN };

  private CategorySeries mSeries = new CategorySeries("");

  private DefaultRenderer mRenderer = new DefaultRenderer();

  private String mDateFormat;


  private GraphicalView mChartView;
  
  public void addseries(int x, String y){
	  mSeries.add(y, x);
      SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
      renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);
      mRenderer.addSeriesRenderer(renderer);
      if (mChartView != null) {
        mChartView.repaint();
  }
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedState) {
    super.onRestoreInstanceState(savedState);
    mSeries = (CategorySeries) savedState.getSerializable("current_series");
    mRenderer = (DefaultRenderer) savedState.getSerializable("current_renderer");
    mDateFormat = savedState.getString("date_format");
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("current_series", mSeries);
    outState.putSerializable("current_renderer", mRenderer);
    outState.putString("date_format", mDateFormat);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.xy_chart);
    Spinner s1=(Spinner)findViewById(R.id.spinner1);
    s1.setVisibility(View.GONE);
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
    mRenderer.setChartTitleTextSize(20);
    mRenderer.setLabelsTextSize(15);
    mRenderer.setLegendTextSize(15);
    mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
    mRenderer.setZoomButtonsVisible(true);
    mRenderer.setStartAngle(90);


    
  }

  @Override
  protected void onResume() {
    super.onResume();
    if (mChartView == null) {
      LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
      mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);
      mRenderer.setClickEnabled(true);
      mRenderer.setSelectableBuffer(10);
      /**
       * information after clicking
       */
      mChartView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
          SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
          if (seriesSelection == null) {
            
          } else {
            Toast.makeText(
                PieChartBuilder.this,
                " Wert=" + seriesSelection.getValue(),
                Toast.LENGTH_SHORT).show();
          }
        }
      });
      
    /**
     * Gets the Name and Amount of the Categories and draws them
     */
    db.open();
  	Cursor cu1 = db.rawQuery(
  			"SELECT Category.Name, Booking.Amount FROM Booking, Booking_Item, Item, Category WHERE Booking.Booking_ID = Booking_Item.Booking AND Booking_Item.Item = Item.Item_ID AND Item.Category = Category.Category_ID AND User_ID="+MainActivity.currentUser.user_ID);
  	db.close();
  	String check="";
  	System.out.println(MainActivity.currentUser.user_ID);
  	 for (int r=0; r<cu1.getCount();r++){
  		if(check.equals(cu1.getString(0))){check=cu1.getString(0);}
  		else {
  			check=cu1.getString(0);
  			Log.d("test", check);
  			Log.d("test2",cu1.getString(1));
  			addseries(Integer.parseInt(cu1.getString(1)),check);}
  		
  	cu1.moveToNext();
  	}
      
      /**
       * deeper info after long pressing
       */
    /*  mChartView.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
          SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
          if (seriesSelection == null) {
            Toast.makeText(PieChartBuilder.this, "No chart element was long pressed",
                Toast.LENGTH_SHORT).show();
            return false; // no chart element was long pressed, so let something
            // else handle the event
          } else {
            Toast.makeText(
                PieChartBuilder.this,
                "Chart element data point index " + seriesSelection.getPointIndex()
                    + " was long pressed", Toast.LENGTH_SHORT).show();
            return true; // the element was long pressed - the event has been
            // handled
          }
        }
      });*/
      layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
          LayoutParams.FILL_PARENT));
    } else {
      mChartView.repaint();
    }
  }
}
